<template>
  <el-dialog :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog" width="900px" @open="open" @close="close" :append-to-body="true" :modal-append-to-body="true">
    <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="100px" :disabled="disableEditSubmit">
      <el-row>
        <el-col :span="12" v-if="!isAdd">
          <el-form-item label="行号" prop="lineno">
            <el-input-number v-model="model.lineno" controls-position="right" :step="1" :min="0" :precision="0" style="width:100%" class="normal" disabled></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="资源编码" prop="resourcecode">
            <el-input v-model="model.resourcecode" :readonly="true" class="is-disabled"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="资源名称" prop="resourcename" :rules="rules.validateRequire">
            <el-input v-model="model.resourcename" :disabled="true" class="tl-ref-selector">
              <el-button slot="append" type="primary" icon="el-icon-search" @click="openPmResourceArchivesRefDialog" v-if="!disableEditSubmit"></el-button>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="单价" prop="unitprice">
            <el-input-number v-model="model.unitprice" controls-position="right" :step="1" :min="0" :precision="2" style="width:94%"></el-input-number>
            <span>元</span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="单位类型" prop="unittype" :rules="rules.validateRequire">
            <el-select v-model="model.unittype" filterable clearable>
              <el-option v-for="item in codeModel.unittypeList" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="数量" prop="num" :rules="model.unittype === '3' ? rules.validateRequire : { required: false }" v-if="model.unittype === '3'">
            <el-input-number v-model="model.num" controls-position="right" :step="1" :min="0" :precision="0" style="width:100%"></el-input-number>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-if="model.unittype !== '3'">
        <el-col :span="12">
          <el-form-item label="开始时间" prop="starttime" :rules="model.unittype !== '3' ? rules.validateRequire : { required: false }">
            <el-date-picker v-model="model.starttime" :type="model.unittype === '2' ? 'datetime' : 'date'" placeholder="开始时间" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="结束时间" prop="endtime" :rules="model.unittype !== '3' ? rules.validateRequire : { required: false }">
            <el-date-picker v-model="model.endtime" :type="model.unittype === '2' ? 'datetime' : 'date'" placeholder="结束时间" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="总金额" prop="totamoney">
            <el-input-number v-model="calcTotalMoney" style="width:94%;" controls-position="right" :precision="2" disabled></el-input-number>
            <span>元</span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="申请说明" prop="applyexplain">
            <el-input v-model="model.applyexplain" type="textarea" :rows="2"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="description">
            <el-input v-model="model.description" type="textarea" :rows="2"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="状态" prop="state">
            <el-input v-model="model.dispstate" disabled></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :type="disableEditSubmit === false ? 'text' : 'primary'" @click="doCancelEdit">取消</el-button>
      <el-button type="primary" @click="doSubmitEdit" :loading="saveLoading" v-if="disableEditSubmit === false">
                <svg-icon icon-class="save" />保存</el-button>
    </div>
    <tl-pm-resource-archives-ref ref="pmResourceArchivesRef" @confirm="getPmResourceArchivesRefData" :isSingleSelect="true"></tl-pm-resource-archives-ref>
  </el-dialog>
</template>

<script>
/** mixins */
import { mixin_edit } from "@tapui/mixins/mixin_edit"
/** 子组件 */
import tlPmResourceArchivesRef from "@/views/ProjectManage/pmResourceArchives/refList"
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from "@tapui/utils/http"
/** VUE配置 */
export default {
  mixins: [mixin_edit],
  components: { tlPmResourceArchivesRef },
  data() {
    return {
      moduleTitle: "资源申请明细",
      urlPrefix: "/pm/resourceApplyLine/",
      url: {
        // 额外的url
      }
    }
  },
  props: {
    parentId: {
      type: [Number, String],
      default: ""
    },
    projectid: {
      type: Number,
      default: 0
    }
  },
  methods: {
    loaded() {
      // 页面加载完毕执行
      if (this.isAdd) {
        this.$set(this.model, "projectid", this.projectid)
        this.$set(this.model, "resourceapplyid", this.parentId)
      }
    },
    /** 打开资源参照 */
    openPmResourceArchivesRefDialog() {
      this.$refs.pmResourceArchivesRef.visible = true
    },
    /** 获取资源参数数据 */
    getPmResourceArchivesRefData(selectionRow, selectKey, selectionValue) {
      const { code, name, price, unittype } = selectionRow
      this.$set(this.model, "resourcecode", code)
      this.$set(this.model, "resourcename", name)
      this.$set(this.model, "unitprice", price)
      this.$set(this.model, "unittype", unittype)
    },
    /** 提交前执行 */
    beforeSubmit() {
      const { starttime, endtime } = this.model
      if (starttime && endtime && starttime >= endtime) {
        this.$tap.alert("开始时间应该小于结束时间", "warning")
        return false
      }
      return true
    }
  },
  computed: {
    calcTotalMoney() {
      const { unitprice, unittype, starttime, endtime } = this.model
      let { num } = this.model
      let totalMoney = 0
      let calcFlag = false
      if (unittype === "1") {
        // 天
        if (starttime && endtime) {
          num = (new Date(endtime) - new Date(starttime)) / (1000 * 60 * 60 * 24) + 1
          calcFlag = true
          this.$set(this.model, "num", null)
        }
      } else if (unittype === "2") {
        // 小时
        if (starttime && endtime) {
          num = Math.ceil((new Date(endtime) - new Date(starttime)) / (1000 * 60 * 60))
          calcFlag = true
          this.$set(this.model, "num", null)
        }
      } else if (unittype === "3") {
        // 数量
        if (unitprice && num !== undefined) {
          calcFlag = true
          this.$set(this.model, "starttime", null)
          this.$set(this.model, "endtime", null)
        }
      }
      if (calcFlag) {
        totalMoney = (unitprice * num).toFixed(2)
        this.$set(this.model, "totamoney", totalMoney)
        return totalMoney
      } else {
        return this.model.totamoney
      }
    }
  }
}
</script>
